#include "DispatcherLogThread.h"
#include "PngTools.h"

namespace android
{

    DispatcherLogThread::DispatcherLogThread(WatermarkManager *manager)
    {
        mManager = manager;
    }

    DispatcherLogThread::~DispatcherLogThread()
    {
    }

    void DispatcherLogThread::start()
    {
        this->run("DispatcherLogThread", PRIORITY_HIGHEST);
    }

    void DispatcherLogThread::onDataReady()
    {
    }

    void DispatcherLogThread::stop()
    {
        requestExitAndWait();
    }

    bool DispatcherLogThread::threadLoop()
    {
        int id, imgW, imgH, x, y, ch, enable;
        int ret, count = 0;
        uint8_t *logImg;
        pic_data out;

        DriverStrInfo devInfo = WMarkImg::getInstance()->getStrInfo();
        while (1)
        {
            ret = decode_png(devInfo.mStr.c_str(), &out);
            if (0 == ret) {
                break;
            }
            if (out.rgba) {
                free(out.rgba);
            }
            usleep(50000);
            if (count > 10) {
                break;
            }
            count++;
            ALOGE("解析 图片(%s)失败-， 重新解析，count=%d", devInfo.mStr.c_str(), count);
        }

        id = devInfo.mCameraID;
        x =  devInfo.mDisplayX;
        y =  devInfo.mDisplayY;
        imgW = out.width;
        imgH = out.height;
        ch = out.channel;
        enable = devInfo.mEnable;

        if (mManager) {
            mManager->onDispatcherLogResult( id, imgW, imgH, x, y, out.rgba, ch, enable);
        }

        if (out.rgba) {
            free(out.rgba);
        }
        return true;
    }

}